﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aij1.Utils;
using Aij1.Model;
using Aij1.BLL;

namespace Aij1.Cms
{
    public partial class Photo : AdminPage
    {
        protected A_Photo P;
        int albumId;
        protected void Page_Load(object sender, EventArgs e)
        {
            int photoId = CommOp.ToInt(Request["Id"]);

            if (photoId <= 0)
            {
                albumId = CommOp.ToInt(Request["AlbumId"]);
                A_Article album = XpoSession.GetObjectByKey<A_Article>(albumId);
                if (album == null)
                {
                    Response.Write("没有找到相册!");
                    Response.End();
                    return;
                }
                P = new A_Photo(XpoSession)
                {
                    AlbumId = album,
                };
            }
            else
            {
                P = XpoSession.GetObjectByKey<A_Photo>(photoId);
            }

            if (P == null)
            {
                Response.Write("没有找到照片!");
                Response.End();
                return;
            }
            if (!IsPostBack)
            {
                AssignPhoto();
            }
        }

        void AssignPhoto()
        {
            txtTitle.Text = P.Title;
            txtIntro.Text = P.Intro;
            txtUrl.Text = P.Url;
            txtSnapShotUrl.Text = P.SnapShotUrl;

            A_Photo next = AlbumData.GetNextPhoto(P);
            A_Photo prev = AlbumData.GetPrevPhoto(P);

            if (next == null)
            {
                lnkNextPhoto.Text = "下一张:没有了";
                lnkNextPhoto.NavigateUrl = "#";
            }
            else
            {
                lnkNextPhoto.Text = "下一张:" + next.Title;
                lnkNextPhoto.NavigateUrl = Request.Path + "?Id=" + next.Oid;
            }

            if (prev == null)
            {
                lnkPrevPhoto.Text = "上一张:没有了";
                lnkPrevPhoto.NavigateUrl = "#";
            }
            else
            {
                lnkPrevPhoto.Text = "上一张:" + prev.Title;
                lnkPrevPhoto.NavigateUrl = Request.Path + "?Id=" + prev.Oid;
            }
        }

        void GetPhoto()
        {
            P.Title = txtTitle.Text.Trim();
            P.Intro = txtIntro.Text.Trim();
            P.Url = txtUrl.Text.Trim();
            P.SnapShotUrl = txtSnapShotUrl.Text.Trim();
            P.OrderId = P.AlbumId.Photos.Count + 1;
        }

        protected void ASPxCallback1_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
        {
            try
            {
                GetPhoto();
                if (String.IsNullOrEmpty(e.Parameter))
                {
                    AlbumData.ReCalcPhoto(P);
                    e.Result = "保存成功!";
                }
                else
                {
                    AlbumData.SaveToLocal(P);
                    e.Result = "保存到本地成功!";
                }
                P.Save();
            }
            catch (Exception ex)
            {
                e.Result = "保存失败!" + ex.Message;
            }
        }

        protected void ASPxCallback1_CustomJSProperties(object sender, DevExpress.Web.ASPxClasses.CustomJSPropertiesEventArgs e)
        {
            e.Properties["cpPhotoId"] = P.Oid;
        }
    }
}